//
//  RBGoodsDetailAppraiseCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/24.
//

#import "RBGoodsDetailAppraiseCell.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import <UIImage+YYAdd.h>

@interface RBGoodsDetailAppraiseCell()

//顶部view
@property (nonatomic, strong) UIView *topView;
//头像
@property (nonatomic, strong) UIImageView *iconImgView;
//名称
@property (nonatomic, strong) UILabel *nameL;
//星级
@property (nonatomic, strong) UILabel *starL;
//
@property (nonatomic, strong) UIView *starV;
//时间
@property (nonatomic, strong) UILabel *timeL;
//内容
@property (nonatomic, strong) UILabel *contentL;

@property (nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBGoodsDetailAppraiseCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColor.whiteColor;
        
        UIView *view = UIView.new;
        [self.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
            make.width.mas_equalTo(kRBMainScreenWidth - Zoom(24));
        }];
        
        [view view_addView:self.topView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(5) bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:self.contentL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:Zoom(5) left:Zoom(18) right:Zoom(18) centerX:0];
    }
    return self;
}

- (void)setCellModel:(RBCellModel *)cellModel
{
    _cellModel = cellModel;
    RBGoodsDetailEvaluateModel *model = cellModel.cellData;
    //是否匿名
    NSString *nameString = model.nickname;
    [self.iconImgView setImageWithUrl:model.avatar placeHolder:nil];
    if ([model.is_anonymous isEqualToString:@"1"]) {
        nameString = @"匿名";
        self.iconImgView.image = [UIImage imageNamed:@"iconDefault"];
    }
    //名字
    self.nameL.text = nameString;
    //星级
    self.starL.text = model.star_num;
    
    CGFloat point = [model.star_num floatValue];
    for (NSInteger i = 1; i <= 5; i++) {
        NSString *imgName = @"";
        if (point >= i) {
            imgName = @"shopStartSel";
        } else if(point > i - 1) {
            imgName = @"shopStartHalf";
        } else {
            imgName = @"shopStart";
        }
        [self.starV h_view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:imgName] withHeight:Zoom(9) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:i == 1? Zoom(5): Zoom(1) right:i == 5? Zoom(5): 0 centerY:YES];
    }
    //时间
    self.timeL.text = model.create_time;
    //内容
    self.contentL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(13)] color:[RBThemeConfig textBlackColor] string:model.content lineSpacing:Zoom(7)];
    }];
}

#pragma mark - Getter

- (UIView *)topView {
    if (!_topView) {
        self.topView = ({
            UIView *view = UIView.new;
            
            [view h_view_addView:({
                UIImageView *imgV = [RanTool imgViewWithMode:2 radius:Zoom(19) imgName:@"iconDefault"];
                self.iconImgView = imgV;
                imgV;
            }) withHeight:Zoom(38) width:Zoom(38) top:0 bottom:0 left:Zoom(15) right:0 centerY:0];
            
            [view h_view_addView:({
                UIView *view = UIView.new;
                
                [view view_addView:({
                    UILabel *nameL = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:@""];
                    self.nameL = nameL;
                    nameL;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
                
                [view view_addView:({
                    UIView *view = UIView.new;
                    [view h_view_addView:({
                        UIView *view = UIView.new;
                        [view addPriority:1];
                        view.backgroundColor = [RBThemeConfig bgGrayColor];
                        [view rbSetCornerRadius:Zoom(7)];
                        self.starV = view;
                        
                        view;
                    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
                    [view h_view_addView:({
                        UILabel *starL = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:@""];
                        self.starL = starL;
                        starL;
                    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(8) right:0 centerY:0];
                    [view h_view_addView:({
                        UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:@""];
                        self.timeL = view;
                        [view addPriority:2];
                        view;
                    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
                    view;
                }) withHeight:Zoom(14) width:CGFLOAT_MIN top:Zoom(7) bottom:0 left:0 right:0 centerX:0];
                
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(7) right:Zoom(15) centerY:YES];
            
            view;
        });
    }
    return _topView;
}

- (UILabel *)contentL
{
    if (!_contentL)
    {
        self.contentL = ({
            UILabel *view = UILabel.new;
            view.numberOfLines = 2;
            view;
        });
    }
    return _contentL;
}

@end
